package algo;

import java.util.ArrayList;
import java.util.HashMap;

import dao.AccessPointsDAO;
import dao.ReferencePointsDAO;

import objects.AccessPoint;
import objects.Client;
import objects.Measurement;
import objects.ReferencePoint;

/**
 * class in charge of calling the differents algorythms
 * @author Micka�l
 *
 */
public class AlgorythmManager  {

	//attributes
	private static HashMap<String, AccessPoint> accessPoints;
	private static ArrayList<ReferencePoint> referencePoints;

	public AlgorythmManager() {
		
		if (accessPoints == null)
			accessPoints = AccessPointsDAO.read();
		if (referencePoints == null)
			referencePoints = ReferencePointsDAO.read();
	}
	
	/**
	 * Compute the position of the client with the frbhm algorythm
	 * @param client
	 * @return
	 */
	public Boolean frbhm(Client client) {
		
		ReferencePoint nearestPoint = MapManager.getReferencePoint(referencePoints, client.getMeasurements());
		
		// Computation of the distances between client and each ap
		ArrayList<Float> distances = new ArrayList<Float>();
		ArrayList<AccessPoint> aps = new ArrayList<AccessPoint>();
		for (Measurement m : client.getMeasurements().values()) {
			AccessPoint ap = accessPoints.get(m.getMacAdress());
			aps.add(ap);
			distances.add(FBCM.computeDistance(client, ap, nearestPoint));
		}
		
		client.setPosition(Trilateration.getResult(aps, distances, 0, 50, 0, 50, (float)0.1));
		
		return true;
	}

	// getters and setters
	public HashMap<String, AccessPoint> getAccessPoints() {
		return accessPoints;
	}

	public void setAccessPoints(HashMap<String, AccessPoint> accessPoints) {
		AlgorythmManager.accessPoints = accessPoints;
	}

	public ArrayList<ReferencePoint> getReferencePoints() {
		return referencePoints;
	}

	public void setReferencePoints(ArrayList<ReferencePoint> referencePoints) {
		AlgorythmManager.referencePoints = referencePoints;
	}
}